package io.renren.modules.contract.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.*;
import io.renren.modules.contract.entity.ContractReceiptEntity;
import io.renren.modules.contract.service.ContractReceiptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.contract.entity.PaymentInfoEntity;
import io.renren.modules.contract.service.PaymentInfoService;


/**
 * 支付详情表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-02 13:35:37
 */
@RestController
@RequestMapping("/contract/paymentinfo")
public class PaymentInfoController {
	@Autowired
	private PaymentInfoService paymentInfoService;
	@Autowired
	private ContractReceiptService contractReceiptService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PaymentInfoEntity> paymentInfoList = paymentInfoService.queryList(query);
		if (paymentInfoList != null && paymentInfoList.size() > 0){
			for (int i = 0 ; i < paymentInfoList.size() ; i++){
				if (paymentInfoList.get(i).getPaymentTime()!= null){
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
					paymentInfoList.get(i).setPaymentTimeStr(formatter.format(paymentInfoList.get(i).getPaymentTime()));
				}
			}
		}
		int total = paymentInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(paymentInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("contract:paymentinfo:info")
	public R info(@PathVariable("id") String id){
		PaymentInfoEntity paymentInfo = paymentInfoService.queryObject(id);
		
		return R.ok().put("paymentInfo", paymentInfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody PaymentInfoEntity paymentInfo) throws ParseException {
		paymentInfo.setId(RandomId.createId()+"");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		paymentInfo.setPaymentTime(sdf.parse(paymentInfo.getPaymentTimeStr()));
		paymentInfo.setPaymentProportion(BigDecimalUtils.stringToDate(paymentInfo.getPaymentMoney(),paymentInfo.getContractTotal()));
		paymentInfoService.save(paymentInfo);
		ContractReceiptEntity contractReceiptEntity = new ContractReceiptEntity();
		contractReceiptEntity.setProjectId(paymentInfo.getProjectId());
		contractReceiptService.maintainPaymentRatio(contractReceiptEntity);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("contract:paymentinfo:update")
	public R update(@RequestBody PaymentInfoEntity paymentInfo){
		paymentInfoService.update(paymentInfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("contract:paymentinfo:delete")
	public R delete(@RequestBody String[] ids){
		paymentInfoService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
